package com.zhiwang.planet.ui.common

import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import com.google.android.material.snackbar.Snackbar
import androidx.core.content.FileProvider
import android.view.View
import com.bumptech.glide.Glide
import com.zhiwang.planet.BuildConfig
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.UtilPresenter
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.activity_photo_preview.*
import java.io.File
import javax.inject.Inject


class PhotoPreviewActivity : BaseActivity() {
    val TAG = "PhotoPreviewActivity"

    companion object {
        fun load(context: Context, url: String, coordinate: NodeCoordinate) {
            load(context, arrayListOf(url), 0, coordinate)
        }

        fun load(context: Context, urls: ArrayList<String>, index: Int, coordinate: NodeCoordinate) {
            val intent = Intent(context, PhotoPreviewActivity::class.java)
            intent.putStringArrayListExtra("ULRS", urls)
            intent.putExtra("Index", index)
            intent.putExtra("Coordinate", coordinate)
            context.startActivity(intent)
        }
    }

    lateinit var coordinate: NodeCoordinate
    var index = 0
    lateinit var urls: ArrayList<String>

    @Inject
    lateinit var presenter: UtilPresenter

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .accountModule(AccountModule(this))
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_photo_preview)
        index = intent.getIntExtra("Index", 0)
        coordinate = intent.getParcelableExtra("Coordinate")
        urls = intent.getStringArrayListExtra("ULRS")

        val requestManager = Glide.with(this)
        requestManager.load(urls[0])
                .into(previewImageView)


        rootView.setOnClickListener {
            suicideAfterTransition()
        }

        previewImageView.setOnClickListener {
            suicideAfterTransition()
        }


    }

    fun saveLocal(v: View) {
        presenter.downLoadFile(urls[0], object : UtilPresenter.CallBack {

            override fun onDownLoadComplete(f: File) {
                if (f.exists()) {
                    MediaStore.Images.Media.insertImage(contentResolver,
                            f.absolutePath, f.name, null)
                    val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    val uri = Uri.fromFile(f)
                    intent.data = uri
                    sendBroadcast(intent)
                    Snackbar.make(rootView, "保存成功", Snackbar.LENGTH_LONG)
                            .setAction("查看") {
                                val fileUri =
                                        FileProvider.getUriForFile(this@PhotoPreviewActivity,
                                                BuildConfig.APPLICATION_ID,
                                                f)

                                //ACTION_VIEW 不支持微信;微信只支持ACTION_SEND，并且从EXTRA_STREAM读取文件
                                val starter = Intent(Intent.ACTION_VIEW)
                                starter.putExtra(Intent.EXTRA_STREAM, fileUri)
                                starter.addFlags(FLAG_GRANT_READ_URI_PERMISSION
                                        or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                                        or FLAG_GRANT_READ_URI_PERMISSION)
                                starter.setDataAndType(fileUri, "image/*")
                                startActivity(starter)
                            }.show()
                }
            }
        })

    }
}
